package com.xexun.gpstracker.indoorlocation;

import java.lang.reflect.Array;
import java.util.*;

public class IndoorLoc {
    private List<Position> position;

    public IndoorLoc() {
        position = new ArrayList<Position>();
        position.add(new Position("A001","XI01",-60,"XI02",-70,"XI03",-80,"XI04",-85));
        position.add(new Position("A002","XI01",-65,"XI02",-75,"XI03",-75,"XI04",-80));
        position.add(new Position("A003","XI01",-70,"XI02",-80,"XI03",-65,"XI04",-75));
        position.add(new Position("A004","XI01",-80,"XI02",-85,"XI03",-60,"XI04",-70));
        position.add(new Position("A005","XI01",-65,"XI02",-65,"XI03",-85,"XI04",-85));
        position.add(new Position("A006","XI01",-75,"XI02",-70,"XI03",-80,"XI04",-80));
        position.add(new Position("A007","XI01",-80,"XI02",-75,"XI03",-75,"XI04",-70));
        position.add(new Position("A008","XI01",-85,"XI02",-80,"XI03",-70,"XI04",-65));
        position.add(new Position("A009","XI01",-70,"XI02",-60,"XI03",-85,"XI04",-80));
        position.add(new Position("A010","XI01",-75,"XI02",-65,"XI03",-80,"XI04",-75));
        position.add(new Position("A011","XI01",-80,"XI02",-70,"XI03",-75,"XI04",-70));
        position.add(new Position("A012","XI01",-85,"XI02",-80,"XI03",-70,"XI04",-60));
    }

    public String indoorLocation(Position pos) {
        String[][] nearName = new String[3][3];

        List<OriData> data = new ArrayList<OriData>();
        int size = pos.data.length;
        for(int i=0; i<size; i++) {
            if(pos.data[i] != null) {
                data.add(pos.data[i]);
            }
        }
        Collections.sort(data, new Comparator<OriData>() {  //将要定位的数据按信号排序
            @Override
            public int compare(OriData lhs, OriData rhs) {
                return ((Integer)(rhs.rssi)).compareTo((Integer)(lhs.rssi));
            }
        });

        for(int m=0; m<3; m++) {
            size = this.position.size();
            List<OriData> absData = new ArrayList<OriData>();
            for (int i = 0; i < size; i++) {
                Position tmpPos = position.get(i);
                int absValue = 999;
                int max = tmpPos.data.length;
                for (int j = 0; j < max; j++) {
                    String findName = data.get(m).name; //定位数据的第m个wifi名字
                    String oriName = tmpPos.data[j].name;   //第i条原始数据中第j个wifi的名字
                    if (findName.equals(oriName)) {
                        absValue = Math.abs(data.get(m).rssi - tmpPos.data[j].rssi);
                        break;
                    }
                }
                absData.add(new OriData(tmpPos.name, absValue)); //保存位置点的名字和信号差值的绝对值
            }
            Collections.sort(absData, new Comparator<OriData>() {  //将绝对值排序
                @Override
                public int compare(OriData lhs, OriData rhs) {
                    return ((Integer) (lhs.rssi)).compareTo((Integer) (rhs.rssi));
                }
            });

            for(int k=0; k<3; k++) {
                nearName[m][k] = absData.get(k).name;
            }
        }

        String[] retStr = new String[3];
        int flag = -1;

        for(int i=0; i<3; i++) {
            for(int j=0; j<3; j++) {
                if(nearName[0][i].equals(nearName[1][j])) {
                    for(int k=0; k<3; k++) {
                        retStr[k] = nearName[0][i];
                        flag = 2;
                        break;
                    }
                    break;
                }
            }
        }
        if(retStr[0] == null) {
            for(int i=0; i<3; i++) {
                for(int j=0; j<3; j++) {
                    if(nearName[0][i].equals(nearName[2][j])) {
                        for(int k=0; k<3; k++) {
                            retStr[k] = nearName[0][i];
                            flag = 1;
                            break;
                        }
                        break;
                    }
                }
            }

            if(retStr[0] == null) {
                for(int i=0; i<3; i++) {
                    for(int j=0; j<3; j++) {
                        if(nearName[1][i].equals(nearName[2][j])) {
                            for(int k=0; k<3; k++) {
                                retStr[k] = nearName[1][i];
                                flag = 0;
                                break;
                            }
                            break;
                        }
                    }
                }
            }
        }

        if(retStr[0] == null || (retStr[0] != null && retStr[1] != null && retStr[2] != null)) {
            return null;
        } else if(retStr[0] != null && retStr[1] == null) {
            return retStr[0];
        } else {
            String[] retStr1 = new String[3];
            for(int i=0; i<3; i++) {
                for(int j=0; j<3; j++) {
                    if(retStr[i] == null)
                        break;
                    if(retStr[i].equals(nearName[flag][j])) {
                        for(int k=0; k<3; k++) {
                            retStr1[k] = nearName[1][i];
                            break;
                        }
                        break;
                    }
                }
            }

            if(retStr1[0] != null && retStr1[1] == null) {
                return retStr1[0];
            } else {
                return null;
            }
        }
    }

    public String test() {
        return indoorLocation(new Position("A000","XI01",-65,"XI02",-65,"XI03",-85,"XI04",-85));
    }

    public class OriData {
        public String name;
        public int rssi;

        public OriData(String dataName,int rssi) {
            this.name = dataName;
            this.rssi = rssi;
        }
    }

    public class Position {
        public String name;
        public OriData[] data;

        public Position(String posName,String dataName1,int rssi1,String dataName2,int rssi2,String dataName3,int rssi3,String dataName4,int rssi4) {
            this.name = posName;
            this.data = new OriData[4];
            this.data[0] = new OriData(dataName1,rssi1);
            this.data[1] = new OriData(dataName2,rssi2);
            this.data[2] = new OriData(dataName3,rssi3);
            this.data[3] = new OriData(dataName4,rssi4);
        }

        public Position(String posName,OriData[] data) {
            this.name = posName;
            this.data = data;
        }
    }
}